package com.egene.tw.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

public class PermissionUtils {
    public static final int P_CODE_CAMERA = 991;
    public static final int P_CODE_STORAGE = 992;
    public static final int P_CODE_BLUE_TOOTH = 993;
    public static final String[] PERMISSIONS_CAMERA = {
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static final String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static final String[] PERMISSIONS_BLUE_TOOTH = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    /**
     * 检测是否已获得所需权限
     *
     * @param permissions
     * @return
     */
    public static boolean checkHasPermission(Context context, String[] permissions) {
        for (String item : permissions) {
            if (ContextCompat.checkSelfPermission(context, item) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 请求权限
     *
     * @param activity
     * @param permissions
     * @param code
     */
    public static void requestPermissions(Activity activity, String[] permissions, int code) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(permissions, code);
        }
    }

    public static void requestPermissions(Fragment activity, String[] permissions, int code) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(permissions, code);
        }
    }


    /**
     * 检测是否已获得所需权限
     *
     * @param grantResults
     * @return
     */
    public static boolean checkGrantResults(Context context, String[] grantResults) {
        PackageManager pm = context.getPackageManager();
        for (String item : grantResults) {
            if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(item, context.getPackageName())) {
                return false;
            }
        }
//        for (int String : grantResults) {
//            if (item != PackageManager.PERMISSION_GRANTED) {
//                return false;
//            }
//        }
        return true;
    }


    /**
     * 检测是否可以访问permissions权限限制的服务
     *
     * @param permissions
     * @param code
     * @return
     */
    public static boolean canAccess(Fragment fragments, String[] permissions, int code) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean hasPermission = PermissionUtils.checkHasPermission(fragments.getActivity(), permissions);
            if (!hasPermission) {
                fragments.requestPermissions(permissions, code);
            }
            return hasPermission;
        } else {
            return true;
        }
    }

    /**
     * 检测是否可以访问permissions权限限制的服务
     *
     * @param permissions
     * @param code
     * @return
     */
    public static boolean canAccess(Activity activity, String[] permissions, int code) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean hasPermission = PermissionUtils.checkHasPermission(activity, permissions);
            if (!hasPermission) {
                activity.requestPermissions(permissions, code);
            }
            return hasPermission;
        } else {
            return true;
        }
    }

}
